<?php

require_once ('..' . DIRECTORY_SEPARATOR . 'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));
include_once(Utils::getPath(array('repositories', 'commentRepository.php')));

$idComment = $_GET['id'];

$userRep = new UserRepository();
$commentRep = new CommentRepository();
$comment = $commentRep->getById($idComment);
$referer = getenv("HTTP_REFERER");

if (isset($idComment) && isset($comment) && $comment->getIdAuthor() == $_SESSION['authenticatedId']) {
    $commentRep->remove($comment);
    Utils::showInfo('Comment deleted!');
    header("Location: $referer");
    exit();
} else if ($comment->getIdAuthor() != $_SESSION['authenticatedId']) {
    Utils::goToErrorPage(StatusCodes::HTTP_FORBIDDEN);
} else {
    Utils::goToErrorPage();
}
?>